package com.kinglone.back.permission.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kinglone.back.permission.dao.PermissionMapper;
import com.kinglone.back.permission.model.Permission;
import com.kinglone.back.permission.service.PermissionService;
import com.kinglone.common.utils.StringUtil;

@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	private PermissionMapper permissionDao;

	@Override
	public List<String> getOperasByRoleIdAndCode(Long roleId, String ModuleCode) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("roleId", roleId);
		params.put("ModuleCode", ModuleCode);
		return permissionDao.getOperasByRoleIdAndCode(params);
	}

	@Override
	public void bindPopedom(String moduleCode, String roleId, String operaCodes) {
		
		//删除与指定角色以及模块的所有的记录
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("moduleCode", moduleCode);
		params.put("roleId", roleId);
		permissionDao.deleteOperas(params);
		//重将用户指定的角色以及权限信息添加至数据库		
		//获取需要绑定的权限信息
		
		if(StringUtil.isNotEmpty(operaCodes)){
			String[] operaCodeList = operaCodes.split(",");
			for(String operaCode : operaCodeList){
				Permission p = new Permission();
				p.setRoleId(roleId);
				p.setModuleCode(moduleCode);
				p.setOperaCode(operaCode);
				permissionDao.save(p);
			}
		}
		
	}


}
